package com.kaishun.study.controller;

import com.kaishun.study.entity.TbUser;
import com.kaishun.study.enums.ResultEnum;
import com.kaishun.study.info.ForgetInfo;
import com.kaishun.study.service.TbUserService;
import com.kaishun.study.utils.*;
import com.kaishun.study.validator.JwtIgnore;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * (TbUser)表控制层
 *
 * @author makejava
 * @since 2020-02-14 12:54:29
 */
@RestController
@RequestMapping("tbUser")
@Slf4j
public class TbUserController {

    @Autowired
    private SmsUtils smsUtils;



    /**
     * 服务对象
     */
    @Resource
    private TbUserService tbUserService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    @ApiOperation("根据id查询单个用户")
    public TbUser selectOne(String id) {
        return this.tbUserService.queryById(id);
    }



    /**************************begin**************************/
    /**
     * @description 获取用户信息
     * @author zhoukaishun
     * @date 2020/2/14 13:04
     */
    @PostMapping("/getList")
    public ResultVO getUserList(){
        log.info("获取所有用户信息");
        List<TbUser> list = tbUserService.getUserList();
        return ResultVOUtil.success(list);
    }

    @PostMapping("/getUserByName")
    @ApiOperation(value = "根据用户名称查询用户集合")
    public ResultVO<List<TbUser>> getUserByName(String name){
        List<TbUser> list = tbUserService.getUserByName(name);
        return ResultVOUtil.success(list);
    }


    @DeleteMapping("/delete")
    public ResultVO deleteUserById(String id){
        log.info("删除用户，id={}",id);
        tbUserService.deleteById(id);
        return ResultVOUtil.success();
    }


    @JwtIgnore
    @PostMapping("/registerUser")
    @ApiOperation("用户注册")
    public ResultVO register(@Valid TbUser user, String verificationCode){
        tbUserService.register(user,verificationCode);
        return ResultVOUtil.success();
    }

    @JwtIgnore
    @PostMapping("/forgetPassword")
    public ResultVO forgetPassword(@Valid ForgetInfo info){
        log.info("忘记密码");
        return tbUserService.forgetPassword(info);
    }

    @PostMapping("/setUser")
    public ResultVO addUser(@Valid TbUser user){
        log.info("注册用户");
        tbUserService.insert(user);
        return ResultVOUtil.success();
    }

    @PutMapping("/setUser")
    public ResultVO updateUser(HttpServletRequest request,@Valid TbUser user){
        log.info("更新用户");
        tbUserService.update(user,request);
        return ResultVOUtil.success();
    }
    @JwtIgnore
    @PostMapping("/login")
    public ResultVO login( HttpServletResponse response, TbUser tbUser){
        log.info("登录");
        String token = tbUserService.login(response,tbUser);
        if(token != null){
            return ResultVOUtil.loginSuccess(token);
        }else{
            return ResultVOUtil.error(ResultEnum.LOGIN_ERROR);
        }
    }

    @PostMapping("/logout")
    public ResultVO postLogout(){
        return ResultVOUtil.success();
    }

    @PostMapping("/info")
    public  ResultVO getUserInfo(HttpServletRequest request){
        log.info("通过token获取用户基本信息");
        TbUser tbUser = tbUserService.getUserInfo(request);
        return ResultVOUtil.success(tbUser);
    }

    @JwtIgnore
    @PostMapping("/sendCode")
    public ResultVO sendSms(String phone){
        log.info("获取验证码");
        if(!MobileUtils.isMobile(phone)){
            log.info("请输入正确的手机号");
            return ResultVOUtil.error(ResultEnum.PHONE_ERROR);
        }
        String responseData = smsUtils.sendMsg(phone,smsUtils.randomNumeric(6));
        return MobileUtils.sendResult(responseData);
    }

    @JwtIgnore
    @PostMapping("/testCode")
    public ResultVO test(String phone,String code){
        log.info("短信验证码发送");
        if(!MobileUtils.isMobile(phone)){
            log.info("请输入正确的手机号");
            return ResultVOUtil.error(ResultEnum.PHONE_ERROR);
        }
        String responseData = smsUtils.sendMsg(phone,code);
        return MobileUtils.sendResult(responseData);

    }


}